package cn.qyd.atomic;

import sun.misc.Unsafe;

/**
 * @author 邱运铎
 * @date 2024-03-25 0:42
 */
public class UnsafeArray {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        String[] array = new String[]{"Hello World", "qiuyunduo", "Java"};
        Unsafe unsafe = UnsafeDemo01.getUnsafe();
        int baseOffset = unsafe.arrayBaseOffset(String[].class);
        System.out.println(baseOffset);
        int arrayIndexScale = unsafe.arrayIndexScale(String[].class);
        System.out.println(arrayIndexScale);

        for (int i = 0; i < array.length; i++) {
            int offset = baseOffset + i * arrayIndexScale;
            System.out.println(offset + " " + unsafe.getObject(array,offset));
        }

    }
}
